package com.alen.sun.controller;

import com.alen.sun.bean.Folk;
import com.alen.sun.bean.PageBean;
import com.alen.sun.bean.Result;
import com.alen.sun.bean.Room;
import com.alen.sun.service.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/room")
@RestController
public class RoomController {
    @Autowired
    private RoomService roomService;

    /**
     * 添加房间
     * @param room 不需要roomStatus
     * @return
     */
    @PostMapping()
    @ResponseBody
    public Result addRoom(@RequestBody Room room) {
        Boolean flag = roomService.addRoom(room);
        if(flag) {
            //true--添加成功
            return Result.success();
        }else {
            //false--存在该房间号添加失败
            return Result.error("该房间号已存在,请勿添加存在的房间");
        }
    }

    /**
     * 修改房间信息
     * @param room
     * @return
     */
    @PutMapping
    @ResponseBody
    public Result updateRoom(@RequestBody Room room) {
        Boolean flag = roomService.updateRoom(room);
        if(flag) {
            return Result.success();
        }else {
            return Result.error("房间信息更新失败");
        }
    }

    /**
     * 删除房间
     * @param room
     * @return
     */
    @DeleteMapping
    @ResponseBody
    public Result deleteRoom(@RequestBody Room room) {
        Boolean flag = roomService.deleteRoom(room);
        if(flag) {
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }


    /**
     * 分页查询
     * @param requestParams
     * @return
     */
    @PostMapping("/selectByPage")
    @ResponseBody
    public Result page(@RequestBody Map<String, Integer> requestParams) {
        Integer page = requestParams.getOrDefault("page", 1);
        Integer pageSize = requestParams.getOrDefault("pageSize", 10);
        PageBean pageBean = roomService.page(page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 分页查询空闲房间
     * @param requestParams
     * @return
     */
    @PostMapping("/selectByPageFreeRoom")
    @ResponseBody
    public Result pageFreeRoom(@RequestBody Map<String, Integer> requestParams) {
        Integer page = requestParams.getOrDefault("page", 1);
        Integer pageSize = requestParams.getOrDefault("pageSize", 10);
        PageBean pageBean = roomService.pageFreeRoom(page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/ids")
    @ResponseBody
    public Result deleteByIds(@RequestBody List<String> ids) {
        Boolean flag = roomService.deleteByIds(ids);
        if(flag){
            return Result.success();
        }else {
            return Result.error("批量删除失败");
        }
    }

    /**
     * 网页搜索功能 聚合模糊查询
     * @param JsonData
     * @return
     */
    @PostMapping("/searchByKeywords")
    @ResponseBody
    public Result searchByKeywords(@RequestBody Map<String,String> JsonData){
        String keywords = JsonData.get("keywords");
        List<Room> rooms = roomService.searchByKeywords(keywords);
        if(!rooms.isEmpty()){
            return Result.success(rooms);
        }else {
            return Result.error("未查询到相关结果");
        }
    }

    /**
     * 获取空闲房间
     * @return
     */
    @GetMapping("/getFreeRoom")
    @ResponseBody
    public Result getFreeRoom(){
        String[] freeRooms = roomService.getFreeRoom();
        return Result.success(freeRooms);
    }

}
